<?php
/** 
*
* @package phpBB3
* 
* @author David Lewis ( http://startrekguide.com ) - Highway of Life
* @author Igor ( http://phpbbmodders.net ) - eviL3
* 
* @version $Id: functions_cvsdb.php 52 2007-05-10 04:31:44Z highwayoflife $
* @copyright (c) 2007 eviL3 and Highway of Life
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
 * install the cvs database
 */
function install_cvsdb()
{
	global $db, $phpbb_root_path, $phpEx, $cvsdb_version;
	// MySQL versions prior to 4.1.3 can’t handle Charsets, so we find out what we are dealing with here
	// we probably don’t 'need' this for the CVSDB, but it’s here anyways if we plan on releasing it in the future.
	switch ($db->sql_layer)
	{
		case 'mysql':
			$db_type = 'mysql_40';
		break;
	
		case 'mysql4':
			if (version_compare($db->mysql_version, '4.1.3', '>='))
			{
				$db_type = 'mysql_41';
			}
			else
			{
				$db_type = 'mysql_40';
			}
		break;
	
		case 'mysqli':
			$db_type = 'mysql_41';
		break;
	}
	
	// create the CVSDB TABLE
	$sql = 'CREATE TABLE ' . CVSDB_TABLE . " (
				cvsdb_id mediumint(8) UNSIGNED NOT NULL auto_increment,
				cvsdb_poster_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
				cvsdb_time int(11) UNSIGNED DEFAULT '0' NOT NULL,
				cvsdb_sql text DEFAULT '' NOT NULL,
				cvsdb_cvs_date int(11) UNSIGNED DEFAULT '0' NOT NULL,
				cvsdb_db_type int(3) UNSIGNED DEFAULT '0' NOT NULL,
				PRIMARY KEY (cvsdb_id),
				KEY cvsdb_poster_id (cvsdb_poster_id)
			)" . (($db_type == 'mysql_41') ? 'CHARACTER SET `utf8` COLLATE `utf8_bin`;' : ';');
	$db->sql_query($sql);
	
	// insert some initial values...
	$sql_ary = array(
		array(
			'cvsdb_poster_id'	=> 2,
			'cvsdb_time'		=> 1177864955,
			'cvsdb_sql'			=> 'SELECT * FROM phpbb_users;',
			'cvsdb_cvs_date'	=> 1174363200
		),
		array(
			'cvsdb_poster_id'	=> 2,
			'cvsdb_time'		=> 1177864955,
			'cvsdb_sql'			=> 'SELECT * FROM phpbb_users WHERE user_id = 2;',
			'cvsdb_cvs_date'	=> 1174333200
		),
	);
	$db->sql_multi_insert(CVSDB_TABLE, $sql_ary);
	
	// Setup $auth_admin class so we can add permission options
	include($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
	$auth_admin = new auth_admin();
	
	// Add permission for manage cvsdb
	$auth_admin->acl_add_option(array(
		'local'		=> array(),
		'global'	=> array('m_manage_cvsdb')
	));
	
	set_config('cvsdb_version', $cvsdb_version, false);
}

/**
 *	COLLABORATORS PLEASE NOTE
 *	If you make any Database changes, run the query to alter/update/add whatever it is here
 *	This is using a sort of cheap version update, but it works quite well.
 *	You can base certain updates off of specific versions here for if someone might have two versions back
 *	Be sure to add any changes to the 'install' above.
 * 
 *	An example of what you would do:
 * 
 *	<code>
 *	if ($config['cvsdb_version'] < '0.0.5')
 *	{
 *		$sql_array[] = array("ALTER TABLE phpbb_cvsdb ADD cvsdb_db_type INT(3) UNSIGNED DEFAULT '0' NOT NULL");
 * 		$sql_array[] = array("more DB queries");
 * 		
 * 		foreach ($sql_array as $sql)
 * 		{
 * 			$db->sql_query($sql);
 * 		}
 *	}
 *	</code>
 * This makes it much easier when collaborating on a project
 * Instead of providing the collaborators with instructions for a Query they should run.
 * Increment the version number at the top of the page and add the alter here and query here and in the install.
 */
function update_cvsdb()
{
	global $phpbb_root_path, $phpEx, $config, $cvsdb_version;
	
	if ($config['cvsdb_version'] < '0.0.2')
	{
		// Setup $auth_admin class so we can add permission options
		include($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
		$auth_admin = new auth_admin();
		
		// Add permission for manage cvsdb
		$auth_admin->acl_add_option(array(
			'local'		=> array(),
			'global'	=> array('m_manage_cvsdb')
		));
	}
	
	// always make sure we end up setting the config value to the latest
	set_config('cvsdb_version', $cvsdb_version, false);
}

/**
 * Check if the date is valid and if it is, convert it to unix timestamp, return true if valid, false if invalid
 *
 * @param string $date
 * @return bool
 */
function cvsdb_validate_date($date)
{
	list ($day, $month, $year) = explode('-', $date);
	
	if (!$day || !$month || !$year)
	{
		return false;
	}
	else if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()))
	{
		return false;
	}
	else if (checkdate($month, $day, $year) === false)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/**
 * Convert a date string to timestamp
 *
 * @param string $date
 * @return int timestamp
 */
function cvsdb_convert_date($date)
{
	list($day, $month, $year) = explode('-', $date);

	return @mktime(0, 0, 0, $month, $day, $year);
}

/**
 * Date select drop down
 *
 * @param string $date
 * @param string $day_select return
 * @param string $month_select return
 * @param string $year_select return
 */
function cvsdb_date_select($date, &$day_select, &$month_select, &$year_select)
{
	// better method...
	list($day, $month, $year) = explode('-', $date);
	
	$day_select = '<option value="0">--</option>';
	for ($i = 1; $i < 32; $i++)
	{
		$selected = ($i == $day) ? ' selected="selected"' : '';
		$day_select .= "<option value=\"$i\"$selected>$i</option>";
	}
	
	$month_select = '<option value="0">--</option>';
	for ($i = 1; $i < 13; $i++)
	{
		$selected = ($i == $month) ? ' selected="selected"' : '';
		$month_select .= "<option value=\"$i\"$selected>$i</option>";
	}
	
	$now = getdate();
	$year_select = '<option value="0">--</option>';
	// the CVS only goes back to 2002 for phpBB3, so we start there
	for ($i = 2002; $i <= $now['year']; $i++)
	{
		$selected = ($i == $year) ? ' selected="selected"' : '';
		$year_select .= "<option value=\"$i\"$selected>$i</option>";
	}
	unset($now);
}

/**
 * Handle the marked entries
 *
 * @param string $mark_action action we are performing
 * @return bool
 */
function handle_marked_cvsdb_entries($mark_action)
{
	global $db, $user, $_POST, $phpbb_root_path, $phpEx;
	
	$entry_ids	= request_var('marked_entry_id', array(0));
	
	if (!sizeof($entry_ids))
	{
		return false;
	}
	
	// for now only use delete marked, but this will expand to a range of functions later on
	// such as changing DB type from MySQLi to Oracle, etc... in one swift action 
	switch ($mark_action)
	{
		case 'delete_marked':
			if (confirm_box(true))
			{
				delete_cvsdb_entry($entry_ids);
				
				$success_msg = (sizeof($entry_ids) == 1) ? 'ENTRY_DELETED' : 'ENTRIES_DELETED';
				$redirect = append_sid("{$phpbb_root_path}cvsdb/index.$phpEx", 'mode=list');
				
				meta_refresh(3, $redirect);
				trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_CVSDB'], '<a href="' . $redirect . '">', '</a>'));
			}
			else
			{
				$s_hidden_fields = array(
					'mark_option'		=> 'delete_marked',
					'submit_mark'		=> true,
					'marked_entry_id'	=> $entry_ids,
				);
				
				confirm_box(false, $user->lang['DELETE_MARKED_ENTRIES'], build_hidden_fields($s_hidden_fields));
			}
		break;
		
		case 'change_to_oracle':
		break;
		case 'change_to_mssql':
		break;
		case 'change_to_firebird':
		break; // etc...
		default:
			return false;
	}
	
	return true;
}

/**
 * Delete a specific cvsdb entry - helper
 *
 * @param int $cvsdb_id entry ID of the CVS Database entry we want to delete
 */
function delete_cvsdb_entry($cvsdb_id)
{
	global $db;
	
	if (!$cvsdb_id)
	{
		return false;
	}
	
	$sql = 'DELETE FROM ' . CVSDB_TABLE . '
			WHERE ' . $db->sql_in_set('cvsdb_id', $cvsdb_id);
	$db->sql_query($sql);
	return true;
}
?>